//
//  WWSideslipViewController.m
//  WWSideslipViewControllerSample
//
//  Created by 王维 on 14-8-26.
//  Copyright (c) 2014年 wangwei. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "WWSideslipViewController.h"
#import "UIViewController+SideslipViewController.h"
#import "IsLeftShowMode.h"
#import "SideBarViewController.h"
#import <objc/runtime.h>

@interface WWSideslipViewController ()

@end

@implementation WWSideslipViewController
{
    BOOL isLeftShow;
}

@synthesize speedf,sideslipTapGes;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)leftIsShow
{
    isLeftShow = YES;
}

-(instancetype)initWithLeftView:(UIViewController *)LeftView
                    andMainView:(UIViewController *)MainView
                   andRightView:(UIViewController *)RighView
                        andBackgroundImage:(UIImage *)image;
{
    self = [super init];
    if(self){
        speedf = 0.8;
        isLeftShow = NO;
        
        mainControl=MainView;
        mainControl.sideViewController=self;
        leftControl=LeftView;
        leftControl.sideViewController=self;
        righControl = RighView;
        righControl.sideViewController=self;
        
        imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [imgview setImage:image];
        [self.view addSubview:imgview];
        
        //--------------------------------------------------
        //滑动手势
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        
        //单击手势
        sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];
        [sideslipTapGes setNumberOfTapsRequired:1];
        
        //中间视图的遮罩
        _mainCoverView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, MainView.view.frame.size.width, MainView.view.frame.size.height)];
        [_mainCoverView addGestureRecognizer:sideslipTapGes];
        //[mainControl.view addGestureRecognizer:pan];
        //--------------------------------------------------

        //左右视图的初始化
        leftControl.view.hidden = YES;
        leftControl.view.alpha=0;
        //leftControl.view.transform = CGAffineTransformMakeScale(sideScale, sideScale);
        leftControl.view.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);

        righControl.view.hidden = YES;
        righControl.view.alpha=0;
        righControl.view.transform = CGAffineTransformMakeScale(sideScale, sideScale);
        righControl.view.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
        
        [self.view addSubview:leftControl.view];
        [self.view addSubview:righControl.view];
        [self.view addSubview:mainControl.view];
        
        //初始化中间view滑到两侧到中间点位置
        leftViewCenter=self.view.frame.size.width+self.view.frame.size.width*miniScale/2-viewGap;
        rightViewCenter=viewGap-self.view.frame.size.width*miniScale/2;
        
        if(righControl==nil)
        {
            rightViewCenter=self.view.frame.size.width/2;
        }
        
        if(leftControl==nil)
        {
            leftViewCenter=self.view.frame.size.width/2;
        }
        
        
    }
    return self;
}



-(void)use
{
    unsigned int count;
    //获取属性列表
    objc_property_t *propertyList = class_copyPropertyList([self class], &count);
    for (unsigned int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertyList[i]);
        NSLog(@"property---->%@", [NSString stringWithUTF8String:propertyName]);
    }
    
    //获取方法列表
    Method *methodList = class_copyMethodList([self class], &count);
    for (int i=0; i<count; i++) {
        Method method = methodList[i];
        NSLog(@"method---->%@", NSStringFromSelector(method_getName(method)));
    }
    
    //获取成员变量列表
    Ivar *ivarList = class_copyIvarList([self class], &count);
    for (int i=0; i<count; i++) {
        Ivar myIvar = ivarList[i];
        const char *ivarName = ivar_getName(myIvar);
        NSLog(@"Ivar---->%@", [NSString stringWithUTF8String:ivarName]);
    }
    
    //获取协议列表
    __unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);
    for (int i=0; i<count; i++) {
        Protocol *myProtocal = protocolList[i];
        const char *protocolName = protocol_getName(myProtocal);
        NSLog(@"protocol---->%@", [NSString stringWithUTF8String:protocolName]);
    }
}
-(void)dealloc
{
}

#pragma mark - 滑动手势

//滑动手势
- (void) handlePan: (UIPanGestureRecognizer *)rec{
    
    CGPoint point = [rec translationInView:self.view];
    

    //根据视图位置判断是左滑还是右边滑动
    if (mainControl.view.frame.origin.x>=0)
    {//显示左视图
        //解决右视图为空时的抖动问题
        if(righControl==nil&& mainControl.view.center.x+point.x*speedf<self.view.frame.size.width/2)
        {
            point.x=(self.view.frame.size.width/2-mainControl.view.center.x)/speedf;
        }
        
        scalef = leftControl==nil&&point.x>0?0:mainControl.view.frame.origin.x/(self.view.frame.size.width-viewGap)*(1-miniScale);
        mainControl.view.center = CGPointMake(leftControl==nil&&point.x>0?leftViewCenter:MIN(leftViewCenter,mainControl.view.center.x + point.x*speedf),
                                      mainControl.view.center.y);
        mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1-scalef,1-scalef);
        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
        
        righControl.view.hidden = YES;
        leftControl.view.hidden = NO;
        leftControl.view.alpha=scalef/(1-miniScale);
        leftControl.view.transform = CGAffineTransformMakeScale(scalef/(1-miniScale)*(1-sideScale)+sideScale,
                                                                scalef/(1-miniScale)*(1-sideScale)+sideScale);
        leftControl.view.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

    }
    else if (mainControl.view.frame.origin.x<0)
    {//显示右视图
        //解决左视图为空时的抖动问题
        if(leftControl==nil&& mainControl.view.center.x+point.x*speedf>self.view.frame.size.width/2)
        {
            point.x=(self.view.frame.size.width/2-mainControl.view.center.x)/speedf;
        }
        
        scalef = righControl==nil&&point.x<0?0:(1-(mainControl.view.frame.origin.x+mainControl.view.frame.size.width-viewGap)/(self.view.frame.size.width-viewGap))*(miniScale-1);
        mainControl.view.center = CGPointMake(righControl==nil&&point.x<0?rightViewCenter:MAX(rightViewCenter,mainControl.view.center.x + point.x*speedf),
                                      mainControl.view.center.y);
        mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1+scalef,1+scalef);
        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
    
        
        righControl.view.hidden = NO;
        leftControl.view.hidden = YES;
        righControl.view.alpha=scalef/(miniScale-1);
        righControl.view.transform = CGAffineTransformMakeScale(scalef/(miniScale-1)*(1-sideScale)+sideScale,
                                                                scalef/(miniScale-1)*(1-sideScale)+sideScale);
        righControl.view.center=CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    }
    
    
    //手势结束后修正位置
    if (rec.state == UIGestureRecognizerStateEnded) {
        if (scalef>(1-miniScale)*0.5)
        {
            [self showLeftView];
        }
        else if (scalef<(miniScale-1)*0.5)
        {
            [self showRighView];
        }
        else
        {
            [self showMainView];
            scalef = 0;
        }
    }

}


#pragma mark - 单击手势
-(void)handeTap:(UITapGestureRecognizer *)tap{
    
    if (tap.state == UIGestureRecognizerStateEnded) {
        [self showMainView];
    }

}

#pragma mark - 修改视图位置
//恢复位置
-(void)showMainView{
    [UIView animateWithDuration:0.3 animations:^(void)
     {
         mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
         if (isLeftShow)
         {
             //mainControl.view.frame = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
             mainControl.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
         }
         else
         {
             mainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
         }
         leftControl.view.alpha=0;
         leftControl.view.transform = CGAffineTransformMakeScale(sideScale, sideScale);
         leftControl.view.center=CGPointMake(self.view.frame.size.width/2*sideScale, self.view.frame.size.height/2);
         righControl.view.alpha=0;
         righControl.view.transform = CGAffineTransformMakeScale(sideScale, sideScale);
         righControl.view.center=CGPointMake(self.view.frame.size.width-self.view.frame.size.width/2*sideScale, self.view.frame.size.height/2);
     }completion:^(BOOL finished)
     {
         leftControl.view.hidden=YES;
         righControl.view.hidden=YES;
         isLeftShow = NO;
         [[NSNotificationCenter defaultCenter] postNotificationName:@"leftShowNo" object:nil];
     }];
    [_mainCoverView removeFromSuperview];
}

//显示左视图
-(void)showLeftView{
    leftControl.view.hidden=NO;
    leftControl.view.transform = CGAffineTransformMakeScale(0.5, 0.5);
    [UIView animateWithDuration:0.3 animations:^(void)
     {
         mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,miniScale,miniScale);
         mainControl.view.center = CGPointMake(leftViewCenter,
                                               [UIScreen mainScreen].bounds.size.height/2);
         leftControl.view.alpha=1.0;
         leftControl.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
         leftControl.view.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
     }completion:^(BOOL finished)
     {
         righControl.view.hidden=YES;
         scalef=(1-miniScale);
         [mainControl.view addSubview:_mainCoverView];
         isLeftShow = YES;
         [[NSNotificationCenter defaultCenter] postNotificationName:@"leftShowYes" object:nil];
     }];
}


//显示右视图
-(void)showRighView{
    righControl.view.hidden=NO;
    [UIView animateWithDuration:0.3 animations:^(void)
    {
        mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,miniScale,miniScale);
        mainControl.view.center = CGPointMake(rightViewCenter,[UIScreen mainScreen].bounds.size.height/2);
        righControl.view.alpha=1.0;
        righControl.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        righControl.view.center=CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
    }completion:^(BOOL finished)
     {
         leftControl.view.hidden=YES;
         scalef=(1-miniScale);
         [mainControl.view addSubview:_mainCoverView];
         
     }];
}

#warning 为了界面美观，所以隐藏了状态栏。如果需要显示则去掉此代码
- (BOOL)prefersStatusBarHidden
{
    return NO; //返回NO表示要显示，返回YES将hiden
}

@end
